<?php
    class Categorie {
        //Alias sur table : cat
        public $id_categorie;
        public $id_parent;
        public $libelle;
        public $date_ajout;
        public $date_modif;
        public $valide;
		
        //Constructeur complet
        public function __construct($id_categorie, $id_parent, $libelle, $date_ajout, $date_modif, $valide) {
            $this->setId($id_categorie);
            $this->setIdParent($id_parent);
            $this->setLibelle($libelle);
            $this->setDateAjout($date_ajout);
            $this->setDateModif($date_modif);
            $this->setValide($valide);
        }

        //Retourne le libellé de la Categorie
        //Paramètre : aucun
        //Retourne (String)
        public function __toString() {
            return $this->libelle;
        }
        
        //Ajoute un objet Categorie à la BDD
        //Paramètre : aucun
        //Retourne (Categorie)
        public function add() {
            $id_parent = null;
            $sql =  'INSERT INTO categorie (id_parent, libelle, date_ajout, date_modif, valide) VALUES
                    ('.intval($this->id_parent).', "'.Db::insertion($this->libelle).'", "'.$this->getDateAjout().'", 
                    "'.$this->getDateModif().'", '.intval($this->valide).')';
            $this->setId(Db::Execute($sql));
            return $this;
        }
		
        //Modifie un objet Categorie dans la BDD
        //Paramètre : aucun
        //Retourne (Categorie)
        public function update() {
            $this->setDateModif(new DateTime());
            $sql =  'UPDATE categorie
                    SET id_parent = '.intval($this->id_parent).',
                        libelle = "'.Db::insertion($this->libelle).'",
                        date_modif = "'.$this->getDateModif().'", 
                        valide = '.intval($this->valide).'
                    WHERE
                        id_categorie = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }

        //Désactive une catégorie dans la BDD
        //Paramètre : aucun
        //Retourne (Categorie)
        public function suppr() {
            $this->setValide(false);
            return $this->update();
        }
	
        //Retourne le parent de cette Categorie
        //Paramètre : aucun
        //Retourne (Categorie)
        public function getParent() {
            $sql =  'SELECT
                        cat.id_categorie as catid_categorie, cat.id_parent as catid_parent, cat.libelle as catlibelle, cat.date_ajout as catdate_ajout,
                        cat.date_modif as catdate_modif, cat.valide as catvalide
                    FROM categorie cat
                    WHERE
                        cat.id_categorie = '.intval($this->getIdParent());
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                return new Categorie($p['catid_categorie'], $p['catid_parent'], $p['catlibelle'], $p['catdate_ajout'], 
                                        $p['catdate_modif'], $p['catvalide']);
            }
            return null;
        }
        
        //Retourne toutes les catégories dont l'objet actuelle est parent
        //Paramètre : aucun
        //Retourne collection de Categorie
        public function getEnfants() {
            $sql =  'SELECT
                        cat.id_categorie as catid_categorie, cat.id_parent as catid_parent, cat.libelle as catlibelle, cat.date_ajout as catdate_ajout,
                        cat.date_modif as catdate_modif, cat.valide as catvalide
                    FROM categorie cat
                    WHERE
                        cat.id_parent = '.intval($this->getId());
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                    $tabRetour[] = new Categorie($p['catid_categorie'], $p['catid_parent'], $p['catlibelle'], $p['catdate_ajout'], 
                                                $p['catdate_modif'], $p['catvalide']);
            }
            return $tabRetour;
        }
		
        //Récupère tout les produits d'une catégorie
        //Paramètre : OPTIONNEL (Booléen)valide
        //Retourne collection de Produit
        public function getProduits($debut = 0, $nombre = 6, $valide = true) {
            $sql =  'SELECT
                        tv.id_tva as tvid_tva, tv.taux as tvtaux, 
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide, 
                        prod.id_produit as prodid_produit, prod.libelle as prodlibelle, prod.reference as prodreference, prod.description as proddescription,
                        prod.description_longue as proddescription_longue, prod.prix_unit as prodprix_unit, prod.promotion as prodpromotion, 
                        prod.vente_privee as prodvente_privee, prod.date_ajout as proddate_ajout, prod.date_modif as proddate_modif, 
                        prod.valide as prodvalide, prod.stock as prodstock
                    FROM produit prod
                    LEFT JOIN tva tv ON prod.id_tva = tv.id_tva
                    LEFT JOIN marque ma ON prod.id_marque = ma.id_marque
                    WHERE 
                        prod.id_produit IN  (SELECT
                                                cp.id_produit
                                            FROM categorie_produit cp
                                            WHERE
                                                cp.id_categorie = '.intval($this->getId()).'
                                            )';
            if($valide) {
                $sql .= ' AND prod.valide = 1';
            }
            
            $sql .= ' LIMIT '.intval($debut).', '.intval($nombre);
            
            $tabRetour = array();
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $marque = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'],
                                    $p['madate_modif'], $p['mavalide']);
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                $tabRetour[] = new Produit($p['prodid_produit'], $marque, $p['prodlibelle'], $p['prodreference'], $p['proddescription'], 
                                            $p['proddescription_longue'], $p['prodstock'], $p['prodprix_unit'], $tva, $p['prodpromotion'], 
                                            $p['prodvente_privee'], $p['proddate_ajout'], $p['proddate_modif'], $p['prodvalide']);
            }
            return $tabRetour;
        }
        
        //Retourne toutes les catégories 
        //Paramètre aucun
        //Retourne collection de Categorie
        public static function getCategories(){
            $sql = 'SELECT
                        cat.id_categorie as catid_categorie, cat.id_parent as catid_parent, cat.libelle as catlibelle, cat.date_ajout as catdate_ajout,
                        cat.date_modif as catdate_modif, cat.valide as catvalide
                    FROM categorie cat';
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $tabRetour[] = new Categorie ($p['catid_categorie'], $p['catid_parent'], $p['catlibelle'], $p['catdate_ajout'], $p['catdate_modif'], 
                                               $p['catvalide']);
            }
            return $tabRetour;
        }

        //Retourne les catégories mère (Avec id_parent = null)
        //Paramètre : OPTIONNEL (Booléen)valide
        //Retourne collection de Categorie
        public static function getCategoriesMere($valide = false) {
            $sql =  'SELECT
                        cat.id_categorie as catid_categorie, cat.id_parent as catid_parent, cat.libelle as catlibelle, cat.date_ajout as catdate_ajout,
                        cat.date_modif as catdate_modif, cat.valide as catvalide
                    FROM categorie cat
                    WHERE
                        cat.id_parent is NULL
                    AND cat.valide = '.intval($valide);
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $tabRetour[] = new Categorie($p['catid_categorie'], $p['catid_parent'], $p['catlibelle'], $p['catdate_ajout'], 
                                            $p['catdate_modif'], $p['catvalide']);
            }
            return $tabRetour;
        }
		
        //Crée un objet Categorie grâce à son ID
        //Paramètre : (int)id
        //Retourne (Categorie)
        public static function rechercheCategorie($id) {
            $sql =  'SELECT
                        cat.id_categorie as catid_categorie, cat.id_parent as catid_parent, cat.libelle as catlibelle, cat.date_ajout as catdate_ajout, 
                        cat.date_modif as catdate_modif, cat.valide as catvalide
                    FROM categorie cat
                    WHERE 
                        cat.id_categorie = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                return new Categorie($p['catid_categorie'], $p['catid_parent'], $p['catlibelle'], $p['catdate_ajout'], $p['catdate_modif'], 
                                    $p['catvalide']);	
            }
            return null;
        }
		
        //GETTERS ET SETTERS
        public function getId() {return $this->id_categorie;}
        public function getIdParent() {return $this->id_parent;}
        public function getLibelle() {return $this->libelle;}
        public function getDateAjout($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_ajout, $format);}
        public function getDateModif($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_modif, $format);}
        public function getValide() {return $this->valide;}

        public function setId($temp) {$this->id_categorie = intval($temp);}
        public function setIdParent($temp) {$this->id_parent = intval($temp);}
        public function setLibelle($temp) {$this->libelle = $temp;}
        public function setDateAjout($temp) {$this->date_ajout = Tools::setterDate($temp);}
        public function setDateModif($temp) {$this->date_modif = Tools::setterDate($temp);}
        public function setValide($temp) {$this->valide = intval($temp);}
		
    }

?>